package spring.boot.adminx.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import spring.boot.adminx.mapper.SysUserMapper;
import spring.boot.adminx.model.SysUser;
import spring.boot.adminx.model.SysUserRole;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

	@Autowired
	private SysUserRoleService sysUserRoleService;


	public SysUser selectByUserName(String username){
		return baseMapper.selectByUserName(username);
	}

	@Transactional
	public String insert(String username, String phone, String email, String[] roles, String pass) {
		QueryWrapper<SysUser> wrapper = new QueryWrapper<>();
		wrapper.eq("username",username);
		List<SysUser> existsList = list(wrapper);
		if (!existsList.isEmpty()){
			return "用户已存在";
		}
		SysUser sysUser = new SysUser();
		sysUser.setId(IdWorker.getId());
		sysUser.setEnable(1);
		sysUser.setCreateTime(System.currentTimeMillis());
		sysUser.setUsername(username);
		sysUser.setPassword(new BCryptPasswordEncoder().encode(pass));
		if (!save(sysUser)) {
			return "用户保存失败";
		}

		if (roles==null || roles.length<=0){
			return "true";
		}

		List<SysUserRole> sysUserRoleList = new ArrayList<>();
		for (String role : roles) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setSysUserId(sysUser.getId());
			sysUserRole.setSysRoleId(Long.parseLong(role));
			sysUserRoleList.add(sysUserRole);
		}
		if(!sysUserRoleService.saveBatch(sysUserRoleList)){
			return "用户保存成功！角色配置失败";
		}
		return "true";
	}

	@Transactional
	public String update(String id, String username, String phone, String email, String[] roles) {
		UpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("username",username);
		updateWrapper.eq("id",id);
		if (!update(updateWrapper)) {
			return "更新用户和用户角色失败";
		}
		if (roles==null || roles.length<=0){
			return "true";
		}



		List<SysUserRole> sysUserRoleList = new ArrayList<>();
		for (String role : roles) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setSysUserId(Long.valueOf(id));
			sysUserRole.setSysRoleId(Long.parseLong(role));
			sysUserRoleList.add(sysUserRole);
		}
		if(!sysUserRoleService.saveBatch(sysUserRoleList)){
			return "用户保存成功！角色配置失败";
		}
		return "true";
	}

	@Transactional
	public String roleUpdate(String id, String[] roles) {
		UpdateWrapper<SysUserRole> removeWrapper = new UpdateWrapper<>();
		removeWrapper.eq("sys_user_id",id);
		sysUserRoleService.remove(removeWrapper);

		if (roles==null || roles.length<=0){
			return "true";
		}
		List<SysUserRole> sysUserRoleList = new ArrayList<>();
		for (String role : roles) {
			SysUserRole sysUserRole = new SysUserRole();
			sysUserRole.setSysUserId(Long.valueOf(id));
			sysUserRole.setSysRoleId(Long.parseLong(role));
			sysUserRoleList.add(sysUserRole);
		}
		if(!sysUserRoleService.saveBatch(sysUserRoleList)){
			return "用户保存成功！角色配置失败";
		}
		return "true";
	}
}
